﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using QuizzMe.Helpers;
using QuizzMe.Model;
using QuizzMe.QuizzServiceRef;

namespace QuizzMe.Pages
{
    /// <summary>
    /// Difficulty page
    /// </summary>
    public partial class Difficulty : BasePage
    {
        private List<QuizzServiceRef.Difficulty> _difficultiesSource;
        private ObservableCollection<CheckableText> _difficulties;

        public ObservableCollection<CheckableText> Difficulties
        {
            get { return _difficulties; }
        }

        public Difficulty()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            Loading = true;

            _difficultiesSource = new List<QuizzServiceRef.Difficulty>();
            _difficulties = new ObservableCollection<CheckableText>();

            ServiceClientHelper.Client.GetDifficultiesCompleted += OnGetDifficultiesCompleted;
            ServiceClientHelper.Client.GetDifficultiesAsync();

            base.OnNavigatedTo(e);
        }

        private void OnGetDifficultiesCompleted(object o, GetDifficultiesCompletedEventArgs args)
        {
            _difficultiesSource = args.Result.ToList();
            foreach (QuizzServiceRef.Difficulty difficulty in args.Result)
            {
                _difficulties.Add(new CheckableText(difficulty.name));
            }
            Loading = false;

            ServiceClientHelper.Client.GetDifficultiesCompleted -= OnGetDifficultiesCompleted;
        }

        private void ButtonSaveClick(object sender, RoutedEventArgs e)
        {
            CheckableText checkableText = _difficulties.FirstOrDefault(c => c.Selected);
            if (checkableText != null)
            {
                IEnumerable<QuizzServiceRef.Difficulty> selectedDifficulties = _difficultiesSource.Where(t => t.name.Equals(checkableText.Text));
                if (selectedDifficulties.Count() > 0)
                {
                    Loading = true;
                    QuizzServiceRef.Difficulty selectedDifficulty = selectedDifficulties.First();
                    ServiceClientHelper.Client.SetDifficultyCompleted += ClientSetDifficultyCompleted;
                    ServiceClientHelper.Client.SetDifficultyAsync(AuthHelper.Get.CurrentUser.User, selectedDifficulty);
                }
            }
        }

        private void ClientSetDifficultyCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            AuthHelper.Get.RefreshUser(extendedUser =>
                                           {
                                               // TODO: Refresh difficulty infos
                                               Loading = false;
                                               this.NavigateTo(new Uri("/Home", UriKind.Relative));
                                           });
            ServiceClientHelper.Client.SetDifficultyCompleted -= ClientSetDifficultyCompleted;
        }

        private void ButtonCancelClick(object sender, RoutedEventArgs e)
        {
            this.NavigateTo(new Uri("/Home", UriKind.Relative));
        }
    }
}
